/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.web.generator.objectinfo;

import com.farmcode.api.runtime.FarmcodeContext;
import com.farmcode.api.runtime.Response;
import com.farmcode.api.runtime.RuntimeServerAPI;
import com.farmcode.api.runtime.RuntimeServerException;
import com.farmcode.api.runtime.metadata.ObjectInfo;
import com.farmcode.api.runtime.metadata.Procedure;
import com.farmcode.api.runtime.metadata.Table;
import com.farmcode.api.runtime.metadata.TableSpc;
import com.farmcode.api.runtime.metadata.View;
import com.farmcode.web.generator.BaseCodeGenerator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;

/**
 *
 * @author Oscar Ivan Prado
 */
public abstract class BaseObjectInfo extends BaseCodeGenerator {
    
    public static final String CURRENT_OBJECT_INFO = "CURRENT_OBJECT_INFO";

    @EJB
    private RuntimeServerAPI runtimeServer;
    private List<ObjectInfo> objectInfos;
    private ObjectInfo currentObjectInfo;

    @PostConstruct
    public void init() {
    }

    public abstract Object getObjectType();

    public abstract void unselectAll();

    public abstract void selectAll();

    public void exploreObjects() {
        Response response;
        try {
            FarmcodeContext context = getContext();
            context.put(FarmcodeContext.CLASS_TYPE, getObjectType());
            response = runtimeServer.getMetadata(context);
            setObjectInfos((List<ObjectInfo>) response.getResult());
        } catch (RuntimeServerException ex) {
            Logger.getLogger(BaseObjectInfo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void exploreObject(ObjectInfo objectInfo) {
        System.out.println("Entro aca!!!!!");
        FarmcodeContext context = getContext();
        context.put(FarmcodeContext.CLASS_TYPE, objectInfo);

        String sessionName = "";

        if (objectInfo instanceof Table) {
            sessionName = new TablesBackingBean().getClass().getCanonicalName() + ".objects";
        } else if (objectInfo instanceof View) {
            sessionName = new ViewsBackingBean().getClass().getCanonicalName() + ".objects";
        } else if (objectInfo instanceof Procedure) {
            sessionName = new ProcedureBackingBean().getClass().getCanonicalName() + ".objects";
        }

        try {
            objectInfo = (ObjectInfo) runtimeServer.getColumn(context).getResult();
            List<ObjectInfo> infos = getSession().getValue(sessionName);

//            for (ObjectInfo info : infos) {
//                if (info.get("name").equals(objectInfo.get("name"))) {
//                    if (objectInfo instanceof Table) {
//                        Table table = ((Table)info);
//                        table.setAttributeChilds(((Table)objectInfo).getAttributeChilds());
//                        getSession().setValue(CURRENT_OBJECT_INFO, table);
//                    }
//                    break;
//                }
//            }
        } catch (RuntimeServerException ex) {
            Logger.getLogger(BaseObjectInfo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void objectSelected(ObjectInfo objectInfo) {
        try {

            String sessionName = "";

            if (objectInfo instanceof Table) {
                sessionName = new TablesBackingBean().getClass().getCanonicalName() + ".objects";
            } else if (objectInfo instanceof View) {
                sessionName = new ViewsBackingBean().getClass().getCanonicalName() + ".objects";
            } else if (objectInfo instanceof Procedure) {
                sessionName = new ProcedureBackingBean().getClass().getCanonicalName() + ".objects";
            }

            List<ObjectInfo> infos = getSession().getValue(sessionName);

//            for (ObjectInfo info : infos) {
//                if (info.get("name").equals(objectInfo.get("name"))) {
//                    if (info.get("used").equals("true")) {
//                        info.put("used", "false");
//                    } else {
//                        info.put("used", "true");
//                    }
//                    break;
//                }
//            }
            getSession().setValue(sessionName, infos);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public void findObjectInfo(FarmcodeContext farmcodeContext) throws RuntimeServerException {
        Response response = runtimeServer.getMetadata(getContext());
        setObjectInfos((List<ObjectInfo>) response.getResult());
    }

    /**
     * @return the objectInfos
     */
    public List<ObjectInfo> getObjectInfos() {
        if (getSession().getValue(getClass().getCanonicalName() + ".objects") != null) {
            objectInfos = getSession().getValue(getClass().getCanonicalName() + ".objects");
        }
        return objectInfos;
    }

    /**
     * @param objectInfos the objectInfos to set
     */
    public void setObjectInfos(List<ObjectInfo> objectInfos) {
        this.objectInfos = objectInfos;
        getSession().setValue(getClass().getCanonicalName() + ".objects", objectInfos);
    }

    /**
     * @return the currentObjectInfo
     */
    public ObjectInfo getCurrentObjectInfo() {
        
        currentObjectInfo = new TableSpc();

        if(getSession().getValue(CURRENT_OBJECT_INFO) != null){
            currentObjectInfo = getSession().getValue(CURRENT_OBJECT_INFO);
        }
        return currentObjectInfo;
    }

    /**
     * @param currentObjectInfo the currentObjectInfo to set
     */
    public void setCurrentObjectInfo(ObjectInfo currentObjectInfo) {
        this.currentObjectInfo = currentObjectInfo;
        getSession().setValue(CURRENT_OBJECT_INFO, currentObjectInfo);
    }
}
